package myUserManage

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/myUserManage"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    myUserManageReq "github.com/flipped-aurora/gin-vue-admin/server/model/myUserManage/request"
)

type MyUserManageService struct {
}

// CreateMyUserManage 创建MyUserManage记录
// Author [piexlmax](https://github.com/piexlmax)
func (myUserService *MyUserManageService) CreateMyUserManage(myUser *myUserManage.MyUserManage) (err error) {
	err = global.GVA_DB.Create(myUser).Error
	return err
}

// DeleteMyUserManage 删除MyUserManage记录
// Author [piexlmax](https://github.com/piexlmax)
func (myUserService *MyUserManageService)DeleteMyUserManage(myUser myUserManage.MyUserManage) (err error) {
	err = global.GVA_DB.Delete(&myUser).Error
	return err
}

// DeleteMyUserManageByIds 批量删除MyUserManage记录
// Author [piexlmax](https://github.com/piexlmax)
func (myUserService *MyUserManageService)DeleteMyUserManageByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]myUserManage.MyUserManage{},"id in ?",ids.Ids).Error
	return err
}

// UpdateMyUserManage 更新MyUserManage记录
// Author [piexlmax](https://github.com/piexlmax)
func (myUserService *MyUserManageService)UpdateMyUserManage(myUser myUserManage.MyUserManage) (err error) {
	err = global.GVA_DB.Save(&myUser).Error
	return err
}

// GetMyUserManage 根据id获取MyUserManage记录
// Author [piexlmax](https://github.com/piexlmax)
func (myUserService *MyUserManageService)GetMyUserManage(id uint) (myUser myUserManage.MyUserManage, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&myUser).Error
	return
}

// GetMyUserManageInfoList 分页获取MyUserManage记录
// Author [piexlmax](https://github.com/piexlmax)
func (myUserService *MyUserManageService)GetMyUserManageInfoList(info myUserManageReq.MyUserManageSearch) (list []myUserManage.MyUserManage, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&myUserManage.MyUserManage{})
    var myUsers []myUserManage.MyUserManage
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Xm != "" {
        db = db.Where("xm LIKE ?","%"+ info.Xm+"%")
    }
    if info.Xb != "" {
        db = db.Where("xb = ?",info.Xb)
    }
    if info.Bm != "" {
        db = db.Where("bm LIKE ?","%"+ info.Bm+"%")
    }
    if info.Zc != "" {
        db = db.Where("zc LIKE ?","%"+ info.Zc+"%")
    }
    if info.Dh != "" {
        db = db.Where("dh LIKE ?","%"+ info.Dh+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
         	orderMap["gh"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	err = db.Limit(limit).Offset(offset).Find(&myUsers).Error
	return  myUsers, total, err
}
